package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.SessionMapper;
import com.csun.cmny.provider.model.constant.ReviseConstant;
import com.csun.cmny.provider.model.domain.OmcReviseVar;
import com.csun.cmny.provider.model.enums.UacRoleCodeEnum;
import com.csun.cmny.provider.service.OmcReviseVarService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-11 09:04
 * @Description:
 */
@Service
@Transactional
public class OmcReviseVarServiceImpl extends BaseService<OmcReviseVar> implements OmcReviseVarService {

    @Autowired
    private SessionMapper sessionMapper;

    /**
     * init revise var
     *
     * @param loginAuthDto the login auth dto
     * @return the int
     */
    @Override
    public Integer init(LoginAuthDto loginAuthDto) {

        List<OmcReviseVar> omcReviseVarList = Lists.newArrayList();

        // service
        OmcReviseVar service = new OmcReviseVar();
        service.setGroupId(loginAuthDto.getGroupId());
        service.setUpdateInfo(loginAuthDto);
        service.setVarKey(ReviseConstant.REVISE_VAR_SERVICE);
        service.setVarValue(UacRoleCodeEnum.SERVICE_MINISTER.getCode());

        // admit
        OmcReviseVar admit = new OmcReviseVar();
        admit.setGroupId(loginAuthDto.getGroupId());
        admit.setUpdateInfo(loginAuthDto);
        admit.setVarKey(ReviseConstant.REVISE_VAR_ADMIT);
        List<String> codeList = Lists.newArrayList();
        codeList.add(UacRoleCodeEnum.RECEPTIONIST.getCode());
        codeList.add(UacRoleCodeEnum.RECEPTION_MINISTER.getCode());
        codeList.add(UacRoleCodeEnum.EVALUATE.getCode());
        admit.setVarValue(StringUtils.join(codeList.toArray(), ","));

        // charge
        OmcReviseVar charge = new OmcReviseVar();
        charge.setGroupId(loginAuthDto.getGroupId());
        charge.setUpdateInfo(loginAuthDto);
        charge.setVarKey(ReviseConstant.REVISE_VAR_CHARGE);
        charge.setVarValue(UacRoleCodeEnum.CHARGE.getCode());

        omcReviseVarList.add(service);
        omcReviseVarList.add(admit);
        omcReviseVarList.add(charge);

        return batchSave(omcReviseVarList);
    }

    /**
     * destroy revise var
     *
     * @param loginAuthDto the login auth dto
     * @return the int
     */
    @Override
    public Integer destroy(LoginAuthDto loginAuthDto) {

        OmcReviseVar omcReviseVar = new OmcReviseVar();
        omcReviseVar.setGroupId(loginAuthDto.getGroupId());

        return delete(omcReviseVar);
    }

    /**
     * query revise var by group id and key
     *
     * @param groupId the group id
     * @param key     the revise var key
     * @return the revise value by key
     */
    @Override
    public String queryVarByGroupIdAndKey(Long groupId, String key) {

        OmcReviseVar omcReviseVar = new OmcReviseVar();
        omcReviseVar.setVarKey(key);
        omcReviseVar.setGroupId(groupId);

        omcReviseVar = selectOne(omcReviseVar);
        if (omcReviseVar == null || PublicUtil.isEmpty(omcReviseVar.getVarValue())) {
            return null;
        }

        return omcReviseVar.getVarValue();
    }

    /**
     * query revise var by group id
     *
     * @param groupId the group id
     * @return the map.
     */
    @Override
    public Map<String, Object> queryVarByGroupId(Long groupId) {

        return sessionMapper.queryReviseVarByGroupId(groupId);
    }
}
